<?php

namespace brand\components;

use brand\logic\BrandHelper;

class Input extends Component
{
    public $children = [];




    public function _init()
    {
        $this->component = 'input';
    }



    public function DbToCurd($field, $props=[])
    {
        $column = \brand\Brand::Builder()->form_props($props)->form_method('editor')->search_condition('like');

        if (isset($props['type']) && $props['type'] == 'textarea'){
            $column->table(false)->search(false);
        }
        return $column;
    }

    /**
     * 构建搜索条件
     * @param $key
     * @param $param
     * @return array|false
     */
    public function buildCondition($key,$param)
    {
        [$field,$key] = $this->getSearchField($key);
        $condition = $this->config['search']['condition'] ?? '';
        if (!$condition) return false;
//        if ($condition == '=') $condition = 'like';
        if ($condition == 'like'){
            $param = "%$param%";
        }
        return [$field . '.' .$key,$condition,$param];
    }

    public function buildConfigColumn($tableConfigValue, $curd_method = 'curd_method')
    {


        $options['type'] =\brand\Brand::Builder('输入框类型','select')->form_options(BrandHelper::formatOptions(['text'=>'text','textarea'=>'textarea','number'=>'number']))->build();



        $cloumn = \brand\Brand::Builder('输入类型','arrays')
            ->form_options([$options])
            ->form_props( [
                'append'    =>  $curd_method != 'curd_method'
            ])
            ->form_visible([['prop' => $curd_method, 'value' => 'Input']])
            ->build();

        $type = $tableConfigValue['form']['props']['type'] ?? 'text';
        return ['column' => $cloumn,'value' => [['type'=>$type]]];

    }

    public function methodFormConf($form,$table,$params)
    {
        $param = $params['param'][0] ?? [];

        $props['type'] = $param['type'] ?? 'text';

        $form['props'] = $props;

        return [$form,$table];
    }




}